package com.mls.spring.spring_demo.helper;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;

import java.lang.reflect.Type;

public class GsonUtils {

    private static Gson mGson = new Gson();

    /**
     * bean->json
     */
    public static <T> String toJson(T bean) {
        return mGson.toJson(bean);
    }

    /**
     * json->bean
     */
    public static <T> T fromJson(String json, Class<T> tClass) {
        return mGson.fromJson(json, tClass);
    }

    /**
     * json->bean
     */
    public static <T> T fromJson(JsonObject jsonObject, Class<T> tClass) {
        return mGson.fromJson(jsonObject, tClass);
    }

    /**
     * json->List<bean>
     */
    public static <T> T fromJson(String json, Type type) throws JsonSyntaxException {
        return mGson.fromJson(json, type);
    }
}
